import os
from shutil import copyfile
from functools import reduce
import Config as conf

copyPath = "Y:/AI_Data/dmData/ad/"



def copyFolder(folder, fileExtensionList, fileList, lastFolderList = []):
    for parent, dirnames, filenames in os.walk(conf.ROOT_DATA_FILE_PATH + folder):
        for filename in filenames:

            lastFolder = os.path.basename(os.path.normpath(parent))
            #print("lastFolder: %s"%(lastFolder))

            shouldCopy = False
            if len(fileExtensionList) != 0:
                shouldCopy = reduce(lambda x,y: x or y, map(lambda x: filename.endswith(x), fileExtensionList))
            if len(fileList) != 0:
                shouldCopy = shouldCopy or reduce(lambda x,y: x or y, map(lambda x: filename == x, fileList))
            if len(lastFolderList) != 0:
                shouldCopy = shouldCopy or reduce(lambda x,y: x or y, map(lambda x: lastFolder == x, lastFolderList))

            if shouldCopy:
                srcFilePath = os.path.join(parent, filename)
                desDir = copyPath + folder
                if folder !=  lastFolder:
                    desDir = os.path.join(desDir, lastFolder)
                desFilePath = os.path.join(desDir, filename)
                print("copy: %s"%(srcFilePath))
                if not os.path.exists(desDir):
                    os.makedirs(desDir)
                copyfile(srcFilePath, desFilePath)

copyFolder('contest_dataset_label', ['db','transformed'], ['appTransformedData', 'pythonData', 'appInstallFeatures.data'])
copyFolder('contest_dataset_app_actions', ['transformed'], [])
copyFolder('contest_dataset_app_usage', ['transformed'], [])
copyFolder('contest_testset', [], ['idData', 'pythonData'])

copyFolder('contest_dataset_shopping', [], [], ['br'])
copyFolder('contest_dataset_newsfeed', [], [], ['br'])
copyFolder('contest_dataset_query', [], [], ['br'])